from PySide6.QtWidgets import QWidget, QDialog, QCheckBox
from PySide6.QtWidgets import QComboBox, QLabel 
from PySide6.QtWidgets import QLineEdit, QPushButton
from PySide6.QtWidgets import QVBoxLayout, QHBoxLayout
from PySide6.QtWidgets import QGridLayout, QTextEdit

class AddPrefixDialog(QDialog):
    def __init__(self, width, height, files, parent=None):
        super().__init__(parent)

        self.prefix = ''
        self.label_choose = QLabel('Choose Prefix: ')
        self.combo_prefix = QComboBox()
        self.enable_custom = QCheckBox('Prefix Preferred Not Found, Custom Prefix Shall Be Used')
        self.label_custom = QLabel('Custom Prefix: ')
        self.input_prefix = QLineEdit()
        self.label_files  = QLabel('Files to add prefix:')
        self.state_output = QTextEdit()
        self.confirm = QPushButton('Confirm')
        self.cancel  = QPushButton('Cancel')

        self.combo_prefix.addItem('STW11')
        self.combo_prefix.addItem('STW12')
        self.combo_prefix.addItem('STW13')
        self.combo_prefix.addItem('STW15')
        self.combo_prefix.addItem('STW17')
        self.combo_prefix.addItem('STW18')
        self.combo_prefix.addItem('STW21')
        self.combo_prefix.addItem('STW1D')
        self.combo_prefix.addItem('STW1E')
        self.combo_prefix.addItem('STW1F')
        self.combo_prefix.addItem('STW1G')
        self.combo_prefix.addItem('STW1I')
        self.combo_prefix.addItem('STW1M')
        self.combo_prefix.addItem('STO1G')
        self.combo_prefix.addItem('STG1J')
        self.input_prefix.setEnabled(False)
        self.state_output.setReadOnly(True)
        self.cancel.clicked.connect(self.close)
        self.confirm.clicked.connect(self.get_prepared)
        self.enable_custom.stateChanged.connect(self.update_custom_prefix)

        for file in files:
            self.state_output.append(file)

        ctbutton = QHBoxLayout()
        ctbutton.addStretch()
        ctbutton.addWidget(self.confirm)
        ctbutton.addStretch()
        ctbutton.addWidget(self.cancel)
        ctbutton.addStretch()
        
        ctmaster = QGridLayout()
        ctmaster.addWidget(self.label_choose, 0, 0, 1, 1)
        ctmaster.addWidget(self.combo_prefix, 0, 1, 1, 3)
        ctmaster.addWidget(self.enable_custom,1, 1, 1, 3)
        ctmaster.addWidget(self.label_custom, 2, 0, 1, 1)
        ctmaster.addWidget(self.input_prefix, 2, 1, 1, 3)
        ctmaster.addWidget(self.label_files,  3, 0, 1, 4)
        ctmaster.addWidget(self.state_output, 4, 0, 1, 4)

        ctdialog = QVBoxLayout(self)
        ctdialog.addLayout(ctmaster)
        ctdialog.addLayout(ctbutton)
        
        self.resize(width, height)

    def update_custom_prefix(self, state):
        sender = self.sender()
        if isinstance(sender, QCheckBox):
            if sender.isChecked():
                self.input_prefix.setEnabled(True)
            else:
                self.input_prefix.setEnabled(False)

    def get_prepared(self):
        if self.enable_custom.isChecked():
            self.prefix = self.input_prefix.text().strip()
        else:
            self.prefix = self.combo_prefix.currentText().strip()

        print("prefix final: ", self.prefix)



#from PySide6.QtWidgets import QApplication
#app = QApplication()
#win = AddPrefixDialog()
#win.resize(500, 600)
#win.show()
#app.exec()
